//
//  HXNetTool.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/29.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXNetTool.h"
#import "AFNetworking.h"
@implementation HXNetTool
+ (void)getNetWithUrl:(NSString *)url body:(id)body Header:(NSDictionary *)header Response:(HXResponeseStyle)response Success:(blockOfSuccess)success Failure:(blockOfFailure)failure {
//    获得网络管理者
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
//    请求头的设置：
    if (header) {
        for (NSString *key in header.allKeys) {
            [manger.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
//    返回值类型
    switch (response) {
        case HXJSON:
            manger.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case HXXML:
            manger.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case HXDATA:
            manger.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
//    设置响应数据类型
    [manger.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
//    ios9----UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    发送请求
    [manger GET:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
    }];
}
+ (void)postNetWithUrl:(NSString *)url body:(id)body BodyStyle:(HXRequestStyle)bodyStyle Header:(NSDictionary *)header Response:(HXResponeseStyle)response Success:(blockOfSuccess)success Failure:(blockOfFailure)failure {
    //1.获取网络管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //2.设置body数据类型:
    switch (bodyStyle) {
        case HXBodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;
        case HXBodyString:
            [manager.requestSerializer setQueryStringSerializationWithBlock:^NSString * _Nonnull(NSURLRequest * _Nonnull request, id  _Nonnull parameters, NSError * _Nullable * _Nullable error) {
                return parameters;
            }];
            break;
        default:
            break;
    }
    //3.请求头的设置:
    if (header) {
        for (NSString *key in header.allKeys) {
            [manager.requestSerializer setValue:header[key] forHTTPHeaderField:key];
        }
    }
    //4.设置返回数据的类型
    switch (response) {
        case HXJSON:
            manager.responseSerializer = [AFJSONResponseSerializer serializer];
            break;
        case HXXML:
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            break;
        case HXDATA:
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        default:
            break;
    }
    
    //5.设置响应数据类型
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css",@"text/plain", @"application/javascript",@"image/jpeg", @"text/vnd.wap.wml", nil]];
    
    //请求https
    manager.securityPolicy.allowInvalidCertificates = YES;
    //6.ios9 --- UTF-8转码
    url = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //7.发送请求
    [manager POST:url parameters:body progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(error);
        NSLog(@"%@", error);
    }];

}

@end
